package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.text.StyledEditorKit.ForegroundAction;
import javax.swing.text.StyledEditorKit.ForegroundAction;

import remoteService.ClientHelper;

import concole.Course;
import concole.IOHelper;
import concole.Student;
import concole.Teacher;
import remoteService.*;

public class TeacherBorad extends OperatingBoard {
	protected Teacher user;
	String userID;
	ArrayList<JButton> operationButtons = new ArrayList<JButton>();
	ArrayList<JPanel> operatingPanels = new ArrayList<JPanel>();
	String currentCourseNumString = "";
	String currentTeacherNumString = "";
	Course course = null;
	Course oldCourse = null;
	ClientHelper cHelper = new ClientHelper();

	public TeacherBorad() {

	}

	public TeacherBorad(String ID, int identity) {

		super(ID, identity);
		userID = ID;
		System.out.println(ID);
		userName = getTeacherNameFromFile(this.ID);
		System.out.println(userName + " ID" + userID);
		infoLabel.setText("             Welcome " + " " + userName + " !");
		/* to create specific operationButtons */
		BoxLayout yLayout = new BoxLayout(operationListPanel, BoxLayout.Y_AXIS);
		operationListPanel.setLayout(yLayout);
		JButton publishButton = new JButton(" Publish    Course    ");
		publishButton.setSize(50, 200);
		publishButton.setFont(middlFont);
		operationListPanel.add(publishButton);
		operationButtons.add(publishButton);
		JButton showCourseBt = new JButton(" Show       Course    ");
		showCourseBt.setSize(50, 200);
		showCourseBt.setFont(middlFont);
		operationListPanel.add(showCourseBt);
		operationButtons.add(showCourseBt);
		JButton UpdateBt = new JButton(" Update    Course    ");
		UpdateBt.setSize(50, 200);
		UpdateBt.setFont(middlFont);
		operationListPanel.add(UpdateBt);
		operationButtons.add(UpdateBt);
		JButton showStudentBt = new JButton(" Show       Student   ");
		showStudentBt.setSize(50, 200);
		showStudentBt.setFont(middlFont);
		operationListPanel.add(showStudentBt);
		operationButtons.add(showStudentBt);
		JButton scoreBt = new JButton(" Record    Score      ");
		scoreBt.setSize(50, 200);
		scoreBt.setFont(middlFont);
		operationListPanel.add(scoreBt);
		operationButtons.add(scoreBt);
		JButton showAllCourseBt = new JButton(" Show  All  Course  ");
		showAllCourseBt.setSize(50, 200);
		showAllCourseBt.setFont(middlFont);
		operationListPanel.add(showAllCourseBt);
		operationButtons.add(showAllCourseBt);
		JButton exitButton = new JButton("---------Exit--------- ");
		exitButton.setSize(50, 200);
		exitButton.setFont(middlFont);
		exitButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				TeacherBorad.this.setVisible(false);
				if (isRemote) {
					System.out.println("Exit to remoteLogin");
					new RemoteLoginUI();
				} else {
					System.out.println("Exit to localLogin");
					new LoginEntry();
				}
			}
		});
		operationListPanel.add(exitButton);

		for (JButton bt : operationButtons) {
			bt.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					displayPanel(arg0);
				}
			});
		}
		/* to create panel for each button */
		// publish panel
		JPanel publishPanel = new JPanel();
		publishPanel.setLayout(null);
		publishPanel.setBounds(0, 0, 600, 500);
		publishPanel.setBackground(Color.green);
		final MyCourseInfoPanel publishInfoPanel = new MyCourseInfoPanel();
		publishPanel.add(publishInfoPanel);
		final JPanel publishButtonsPanel = new JPanel();
		publishButtonsPanel.setBackground(Color.green);
		publishButtonsPanel.setBounds(0, 350, 600, 40);
		publishButtonsPanel.setLayout(null);
		FormatHintButton helpBt1 = new FormatHintButton(410, 0, 150, 40);
		helpBt1.setFont(middlFont);
		publishButtonsPanel.add(helpBt1);
		JButton yesBT1 = new JButton("Yes");
		yesBT1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String newTotalMessage = publishInfoPanel.getCourseMessage();
				boolean formatRight = checkCourseMessageFormat(newTotalMessage);
				if (!formatRight) {
					return;
				}

				if (!isRemote) {
					course = new Course(newTotalMessage);
				} else {
					course = new Course(newTotalMessage, true);
				}
				boolean NumExistence = checkCouseNum(course.getCourseNumber());
				System.out.println("In teacherBoard numExistence:"
						+ NumExistence);
				if (NumExistence) {
					JOptionPane.showMessageDialog(null,
							"Sorry the course Number has existend");

					return;
				}

				publishCourse(course);
			}
		});
		yesBT1.setBounds(50, 0, 100, 40);
		JButton resetBt1 = new JButton("Reset");
		resetBt1.setBounds(170, 0, 100, 40);
		resetBt1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				publishInfoPanel.resetAll(true, null);
			}
		});
		JButton canclBt1 = new JButton("Cancel");
		canclBt1.setBounds(290, 0, 100, 40);
		canclBt1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				displayPanel.removeAll();
				TeacherBorad.this.repaint();
			}
		});
		yesBT1.setFont(middlFont);
		resetBt1.setFont(middlFont);
		canclBt1.setFont(middlFont);
		publishButtonsPanel.add(yesBT1);
		publishButtonsPanel.add(resetBt1);
		publishButtonsPanel.add(canclBt1);
		publishPanel.add(publishButtonsPanel);
		operatingPanels.add(publishPanel);

		// show course panel
		JPanel showCPanel = new JPanel();
		showCPanel.setLayout(null);
		showCPanel.setBounds(0, 0, 600, 500);
		showCPanel.setBackground(Color.green);
		final JTextField courseNoField = new JTextField();
		courseNoField.setBounds(50, 50, 100, 25);
		final MyCourseInfoPanel showCourseInfoPanel = new MyCourseInfoPanel();
		showCourseInfoPanel.setBounds(0, 100, 600, 300);
		showCourseInfoPanel.setAssistantLabel("AssistName");
		JButton yesButton = new JButton("Search");
		yesButton.setFont(middlFont);
		yesButton.setBounds(160, 50, 100, 25);
		yesButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (courseNoField.getText().length() == 0) {
					JOptionPane.showMessageDialog(null, "Input is null!");
					return;
				}

				String[] resultStrings = showCourse(courseNoField.getText());
				if (resultStrings == null) {
					JOptionPane.showMessageDialog(null, "courseIsNotFind!");
					return;
				}
				boolean hasRight = hasRight(courseNoField.getText());
				if (!hasRight) {
					JOptionPane.showMessageDialog(null,
							"Sorry it is not your course");
					return;
				}
				showCourseInfoPanel.resetAll(false, resultStrings);
			}
		});

		showCPanel.add(yesButton);
		showCPanel.add(courseNoField);
		showCPanel.add(showCourseInfoPanel);
		operatingPanels.add(showCPanel);

		// updateCourse Panel

		JPanel updatePanel = new JPanel();
		updatePanel.setLayout(null);
		updatePanel.setBounds(0, 0, 600, 500);
		updatePanel.setBackground(Color.green);

		final JTextField courseNoFieldUpdate = new JTextField();
		courseNoFieldUpdate.setBounds(50, 50, 100, 25);
		final MyCourseInfoPanel courseInfoUpdateP = new MyCourseInfoPanel();
		courseInfoUpdateP.setBounds(0, 100, 600, 300);
		FormatHintButton helpButton2 = new FormatHintButton(410, 0, 150, 40);

		JButton searchBtUpdate = new JButton("Search");
		searchBtUpdate.setFont(middlFont);
		searchBtUpdate.setBounds(160, 50, 100, 25);
		searchBtUpdate.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

				if (courseNoFieldUpdate.getText().length() == 0) {
					JOptionPane.showMessageDialog(null,
							"sorry please input the course number to serrch");
					return;
				}
				String[] resultStrings = showCourse(courseNoFieldUpdate
						.getText());
				if (resultStrings == null) {
					JOptionPane.showMessageDialog(null,
							"Sorry the course not found!");
					return;
				}
				boolean hasRight = hasRight(courseNoFieldUpdate.getText());
				if (!hasRight) {
					JOptionPane.showMessageDialog(null,
							"Sorry it is not your course");
					return;
				}
				courseInfoUpdateP.resetAll(false, resultStrings);
				TeacherBorad.this.currentCourseNumString = courseNoFieldUpdate
						.getText();
				TeacherBorad.this.currentTeacherNumString = new Course(
						courseInfoUpdateP.getCourseMessage())
						.getTeacherNumString();

				if (!isRemote) {
					oldCourse = new Course(courseInfoUpdateP.getCourseMessage());
				} else {
					oldCourse = new Course(
							courseInfoUpdateP.getCourseMessage(), true);
				}
				System.out.println(oldCourse.toTransferString());
			}
		});
		JPanel updateButtonsPanel = new JPanel();
		updateButtonsPanel.setBackground(Color.green);
		updateButtonsPanel.setBounds(0, 420, 600, 40);
		updateButtonsPanel.setLayout(null);
		updateButtonsPanel.add(helpButton2);
		JButton yesBT2 = new JButton("Save");
		yesBT2.setBounds(50, 0, 100, 40);
		yesBT2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String newTotalMessage = courseInfoUpdateP.getCourseMessage();
				String newNum = courseInfoUpdateP.getNumField();
				boolean isNumChanged = (!TeacherBorad.this.currentCourseNumString
						.equals(newNum));
				Course newCourse = null;
				if (!isRemote) {
					newCourse = new Course(newTotalMessage);
				} else {
					newCourse = new Course(newTotalMessage, true);
				}

				boolean formatRight = checkCourseMessageFormat(newTotalMessage);
				if (!formatRight) {
					return;
				}

				if (isNumChanged) {

					boolean NumExistence = checkCouseNum(newCourse
							.getCourseNumber());
					System.out.println("IsNumExistant?:" + NumExistence);
					if (NumExistence) {
						JOptionPane
								.showMessageDialog(null,
										"Sorry the new course number has occupied by other courses");
						return;
					}
				}

				boolean isTeacherChanged = !newCourse.getTeacherNumString()
						.equals(TeacherBorad.this.currentTeacherNumString);
				System.out.println("newnum:" + newNum + isTeacherChanged);
				updateCourseList(oldCourse, newCourse);
				if (isTeacherChanged) {
					/*
					 * String nameString = getTeacherNameFromFile(newCourse
					 * .getTeacherNumString()); String messageString =
					 * newTotalMessage.replace(
					 * newCourse.getTeacherNameString(), nameString); if
					 * (isRemote) { newCourse = new Course(messageString, true);
					 * } else { newCourse = new Course(messageString); }
					 */

					if (isNumChanged) {
						updateTeacherListFile(oldCourse, newCourse, true, true);
						updateStudentListFile(oldCourse, newCourse);

					} else {
						updateTeacherListFile(oldCourse, newCourse, false, true);

					}
				} else if (isNumChanged) {
					updateTeacherListFile(oldCourse, newCourse, true, false);
					updateStudentListFile(oldCourse, newCourse);

				}

			}
		});
		JButton resetBt2 = new JButton("Reset");
		resetBt2.setBounds(170, 0, 100, 40);
		resetBt2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				courseInfoUpdateP.resetAll(true, null);
			}
		});
		JButton canclBt2 = new JButton("Cancel");
		canclBt2.setBounds(290, 0, 100, 40);
		canclBt2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				displayPanel.removeAll();
				TeacherBorad.this.repaint();
			}
		});
		yesBT2.setFont(middlFont);
		resetBt2.setFont(middlFont);
		canclBt2.setFont(middlFont);
		updateButtonsPanel.add(yesBT2);
		updateButtonsPanel.add(resetBt2);
		updateButtonsPanel.add(canclBt2);
		updatePanel.add(searchBtUpdate);
		updatePanel.add(courseNoFieldUpdate);
		updatePanel.add(courseInfoUpdateP);
		updatePanel.add(updateButtonsPanel);
		operatingPanels.add(updatePanel);
		// show Student
		JPanel showStudentPanel = new JPanel();
		showStudentPanel.setBackground(Color.BLUE);
		showStudentPanel.setBounds(0, 0, 600, 500);
		showStudentPanel.setLayout(null);
		final JTextField studentNoField = new JTextField();
		studentNoField.setBounds(50, 50, 100, 25);

		String[][] tableDataStrings = { { "1", "2" }, };
		String[] columnTitle = new String[] { "CourseNumber", "studentNo." };
		final DefaultTableModel tableModel1 = new DefaultTableModel(
				tableDataStrings, columnTitle);
		final JTable jTable = new JTable(tableModel1);
		jTable.setBackground(Color.yellow);
		JScrollPane jScrollPane = new JScrollPane();
		jScrollPane.setViewportView(jTable);
		jScrollPane.setBounds(50, 100, 500, 300);
		JButton searchButton = new JButton("Search");
		searchButton.setFont(middlFont);
		searchButton.setBounds(160, 50, 100, 25);
		searchButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stubT
				if (studentNoField.getText().length() == 0) {
					JOptionPane.showMessageDialog(null,
							"student number is needed!");
					return;
				} else if (!studentNoField.getText().matches("[0-9]*")) {
					JOptionPane.showMessageDialog(null,
							"Sorry error format, only number allowed");
					return;
				}
				boolean hasRight = hasRight(studentNoField.getText());
				if (!hasRight) {
					JOptionPane.showMessageDialog(null,
							"Sorry it is not your course");
					return;
				}
				String[] allStudentString = showStudent(studentNoField
						.getText());
				if (allStudentString == null) {
					JOptionPane.showMessageDialog(null,
							"Sorry not found the record");
					return;
				}
				tableModel1.setRowCount(0);
				for (int i = 0; i < allStudentString.length; i++) {
					String[] arr = allStudentString[i].split(" ");
					tableModel1.addRow(arr);
				}
				jTable.invalidate();
				jTable.updateUI();
			}
		});
		showStudentPanel.add(studentNoField);
		showStudentPanel.add(searchButton);
		showStudentPanel.add(jScrollPane);
		operatingPanels.add(showStudentPanel);
		// record score
		JPanel recordPanel = new JPanel();
		recordPanel.setBackground(Color.magenta);
		recordPanel.setBounds(0, 0, 600, 500);
		recordPanel.setLayout(null);
		final JTextField courseNumberField = new JTextField();
		courseNumberField.setBounds(50, 50, 100, 25);
		String[][] tableDataStrings2 = { { "1", "2", "3" }, };
		String[] columnTitle2 = new String[] { "studentName", "studentNo.",
				"scores" };
		final DefaultTableModel tableModel2 = new DefaultTableModel(
				tableDataStrings2, columnTitle2);
		final JTable jTable2 = new JTable(tableModel2);
		jTable2.setBackground(Color.yellow);
		JScrollPane jScrollPane2 = new JScrollPane(jTable2);
		jScrollPane2.setBounds(50, 100, 500, 250);
		JButton searchButton2 = new JButton("Search");
		searchButton2.setFont(middlFont);
		searchButton2.setBounds(160, 50, 100, 25);
		searchButton2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

				if (courseNumberField.getText().length() == 0) {
					JOptionPane.showMessageDialog(null,
							"course number is needed");
					return;
				} else if (!courseNumberField.getText().matches("[0-9]*")) {
					JOptionPane.showMessageDialog(null,
							"sorry !format error just number allowed");
					return;
				}
				boolean hasRight = hasRight(courseNumberField.getText());
				if (!hasRight) {
					JOptionPane.showMessageDialog(null,
							"Sorry it is not your course");
					return;
				}
				String[] allStudentString = showStudent(courseNumberField
						.getText());
				tableModel2.setRowCount(0);
				if (allStudentString == null) {
					JOptionPane.showMessageDialog(null,
							"Sorry the record not found");
					return;
				}
				for (int i = 0; i < allStudentString.length; i++) {
					String[] arr = new String[3];
					arr[0] = allStudentString[i].split(" ")[0];
					arr[1] = allStudentString[i].split(" ")[1];
					arr[2] = "";
					tableModel2.addRow(arr);
				}
				jTable2.invalidate();
				jTable2.updateUI();
			}
		});

		recordPanel.add(courseNumberField);
		recordPanel.add(searchButton2);
		recordPanel.add(jScrollPane2);
		JPanel scoreButtons = new JPanel();
		scoreButtons.setBackground(Color.magenta);
		scoreButtons.setBounds(0, 400, 600, 25);
		scoreButtons.setLayout(null);
		JButton yesBT3 = new JButton("Yes");
		yesBT3.setBounds(50, 0, 100, 25);
		yesBT3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int lineNum = tableModel2.getRowCount();
				String[] standardStrings = new String[lineNum];
				for (int i = 0; i < lineNum; i++) {
					standardStrings[i] = tableModel2.getValueAt(i, 1) + " "
							+ courseNumberField.getText() + " "
							+ tableModel2.getValueAt(i, 2);
					System.out.println(standardStrings[i]);
				}
				recordScore(standardStrings);
			}
		});
		JButton resetBt3 = new JButton("Reset");
		resetBt3.setBounds(200, 0, 100, 25);
		resetBt3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

			}
		});
		JButton canclBt3 = new JButton("Cancel");
		canclBt3.setBounds(350, 0, 100, 25);
		canclBt3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				displayPanel.removeAll();
				TeacherBorad.this.repaint();
			}
		});
		yesBT3.setFont(middlFont);
		resetBt3.setFont(middlFont);
		canclBt3.setFont(middlFont);
		scoreButtons.add(yesBT3);
		scoreButtons.add(resetBt3);
		scoreButtons.add(canclBt3);

		recordPanel.add(scoreButtons);
		operatingPanels.add(recordPanel);

		// showAllCourse

		JPanel showAllCoursePanel = new JPanel();
		showAllCoursePanel.setBackground(Color.BLUE);
		showAllCoursePanel.setBounds(0, 0, 600, 500);
		showAllCoursePanel.setLayout(null);

		String[][] tableDataStrings3 = { { "default", "1", "1", "1", "1", "1",
				"1", "1", "1", "1", "1", "2" }, };
		String[] columnTitle3 = new String[] { "No.", "Name", "TeacherName",
				"TeacherNum", "Properties", "Hours", "Credit", "StartWeek",
				"EndWeek", "Time", "Grade", "Assistent" };
		final DefaultTableModel tableModel3 = new DefaultTableModel(
				tableDataStrings3, columnTitle3);

		final JTable jTable3 = new JTable(tableModel3);
		jTable3.setBackground(Color.yellow);
		jTable3.setPreferredScrollableViewportSize(new Dimension(1000, 300));
		jTable3.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		int[] width={45,100,150,100,40,30,30,50,50,80,80,100};
		for (int i = 0; i < 12; i++) {
			jTable3.getColumnModel().getColumn(i).setPreferredWidth(width[i]);
		}
		JScrollPane jScrollPane3 = new JScrollPane();

		jScrollPane3.setViewportView(jTable3);
		jScrollPane3.setBounds(50, 100, 500, 300);
		// Dimension dim3=new Dimension(500,300);
		// jTable3.setPreferredScrollableViewportSize(dim3);
		jScrollPane3
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

		JButton showCourseOKButton = new JButton("Okay");
		showCourseOKButton.setFont(middlFont);
		showCourseOKButton.setBounds(200, 50, 100, 25);
		showCourseOKButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stubT
				String[] allCourseStrings = showAllCourse(TeacherBorad.this.ID);
				tableModel3.setRowCount(0);
				for (int i = 0; i < allCourseStrings.length; i++) {
					String[] arr = new String[12];
					arr = allCourseStrings[i].split(" ");
					System.out.println(arr[0] + "%" + arr[1] + "%" + arr[2]);
					tableModel3.addRow(arr);
				}
				jTable3.invalidate();
				jTable3.updateUI();
			}
		});

		showAllCoursePanel.add(showCourseOKButton);
		showAllCoursePanel.add(jScrollPane3);
		operatingPanels.add(showAllCoursePanel);
	}

	protected void updateStudentListFile(Course oldCourse, Course newCourse) {
		String oldNum = oldCourse.getCourseNumber();
		String newNum = newCourse.getCourseNumber();
		IOHelper.replaceOneTermOfLine("courseSelected.txt", oldNum, newNum,
				oldNum, 0, 0);
		IOHelper.replaceOneTermOfLine("Scores.txt", oldNum, newNum, oldNum, 1,
				1);

	}

	protected void updateTeacherListFile(Course oldCourse, Course newCourse,
			boolean ifCnumChange, boolean ifTChange) {
		// TODO Auto-generated method stub
		if (ifCnumChange) {
			for (int i = 0; i < oldCourse.teachers.size(); i++) {
				IOHelper.replaceOneTermOfLine("teachersList.txt", oldCourse
						.getCourseNumber(), newCourse.getCourseNumber(),
						oldCourse.teachers.get(i).getNum(), 2, 0);
			}
		}
		if (ifTChange) {
			for (int i = 0; i < oldCourse.teachers.size(); i++) {
				IOHelper.removeOneTermFromLine("teachersList.txt", newCourse
						.getCourseNumber(), oldCourse.teachers.get(i).getNum(),
						2, 0);
			}
			for (int i = 0; i < newCourse.teachers.size(); i++) {
				IOHelper.addToLineEnd("teachersList.txt", newCourse.teachers
						.get(i).getNum(), 0, newCourse.getCourseNumber());
			}
		}
	}

	protected String getTeacherNameFromFile(String numString) {
		// TODO Auto-generated method stub
		String resultString;
		String[] strings = null;
		StringBuffer bf = new StringBuffer();
		if (numString.contains("&")) {
			strings = numString.split("&");
			for (int i = 0; i < strings.length; i++) {
				bf.append((new Teacher(strings[i]).setNameFromFile()) + "&");
			}
		} else {
			bf.append((new Teacher(numString).setNameFromFile()));
		}
		resultString = bf.toString();
		if (resultString.trim().endsWith("&")) {
			resultString = resultString.substring(0,
					resultString.lastIndexOf("&"));
		}
		System.out.println(resultString);
		return resultString;
	}

	protected boolean checkCourseMessageFormat(String newTotalMessage) {
		// TODO Auto-generated method stub
		boolean formatRight = true;
		String[] messageStrings = newTotalMessage.trim().split(" ");
		if (!messageStrings[0].matches("[0-9]*")) {
			JOptionPane.showMessageDialog(null, "Sorry courseNum format error");
			return false;
		}
		if (!messageStrings[1].matches("([a-z]||[0-9]||[A-Z])*")) {
			JOptionPane.showMessageDialog(null, "courseName format error");
			return false;
		}
		if (!messageStrings[2].matches("([a-z]||[A-Z]||&)*")) {
			JOptionPane.showMessageDialog(null,
					"teacher's Name format error\nPay attention that two "
							+ "teachcerNUm should be associated with&");
			return false;
		}
		if (!messageStrings[3].matches("([0-9]||&)*")) {
			JOptionPane.showMessageDialog(null,
					"teacher's Number format error\nPay attention that two "
							+ "teachcerName should be associated with&");
			return false;
		}
		if (!messageStrings[4].matches("true||false")) {
			JOptionPane.showMessageDialog(null, "only allowed true or false");
			return false;
		}
		if (!messageStrings[5].matches(".*")) {
			JOptionPane.showMessageDialog(null, "1check your format please");
			return false;
		}
		if (!messageStrings[6].matches("[1-9]")) {
			JOptionPane.showMessageDialog(null, "2heck your format please");
			return false;
		}
		if (!messageStrings[7].matches("[1-9][0-9]*")) {
			JOptionPane.showMessageDialog(null, "3check your format please");
			return false;
		}
		if (!messageStrings[8].matches("[1-9][0-9]*")) {
			JOptionPane.showMessageDialog(null, "4check your format please");
			return false;
		}
		if (!messageStrings[9].matches(".*")) {
			JOptionPane.showMessageDialog(null, "5check your format please");
			return false;
		}
		if (!messageStrings[10].matches(".*")) {
			JOptionPane.showMessageDialog(null, "6check your format please");
			return false;
		}
		if (!messageStrings[11].matches("..[0-9]*||..([a-z]||[A-Z])*")) {
			JOptionPane.showMessageDialog(null, "7check your format please");
			return false;
		}
		return formatRight;

	}

	protected void publishCourse(Course course) {
		// TODO Auto-generated method stub
		System.out.println(course.toStoreString() + " \n"
				+ course.teachers.toString());
		IOHelper.addOneLine("courseList.txt", course.toStoreString());
		/*
		 * refresh the file of teacher and add the courseNum to the specific
		 * record
		 */
		for (int i = 0; i < course.teachers.size(); i++) {
			IOHelper.addToLineEnd("teachersList.txt", course.teachers.get(i)
					.getNum(), 0, course.getCourseNumber());
		}

	}

	protected boolean checkCouseNum(String courseNumber) {
		// TODO Auto-generated method stub
		System.out.println("In CheckCouseNum:" + courseNumber);
		String recordString = IOHelper.findOneRecord(0, "courseList.txt",
				courseNumber);
		if (recordString == null) {
			return false;
		} else if (recordString.length() == 0) {
			return false;
		}
		return true;
	}

	protected void updateCourseList(Course oldCourse, Course newCourse) {
		// TODO Auto-generated method stub
		System.out.println(oldCourse.toStoreString() + "\n"
				+ newCourse.toStoreString());
		IOHelper.replaceOneRecord("courseList.txt", oldCourse.toStoreString()
				.trim(), newCourse.toStoreString().trim());
		System.out.println("updateCourseLocal");
	}

	protected String[] showAllCourse(String teacherNum) {
		// TODO Auto-generated method stub
		String resultString = IOHelper.findAllRegionMatches(3,
				"CourseList.txt", teacherNum);
		System.out.println(resultString);
		String[] resultStrings = resultString.trim().split("\n");
		return resultStrings;

	}

	protected String[] showStudentsScore(String courseNum) {
		// TODO Auto-generated method stub
		String[] resultStrings = null;
		String string = IOHelper.findAllMatchInfo(1, "Scores.txt", courseNum);
		resultStrings = string.split("\n");

		return resultStrings;
	}

	protected String[] showStudent(String courseNum) {
		// TODO Auto-generatedmethod stub
		String[] resultStrings = null;
		String string = IOHelper.findAllMatchInfo(0, "courseSelected.txt",
				courseNum);
		if (string == null) {
			System.out.println("null");
			return null;
		} else if (string.length() == 0) {
			System.out.println("length0");// length ==0;
			return null;
		}
		resultStrings = string.split("\n");

		return resultStrings;
	}

	protected void recordScore(String[] tostoreStrings) {
		// TODO Auto-generated method stub
		for (int i = 0; i < tostoreStrings.length; i++) {
			IOHelper.addOneLine("Scores.txt", tostoreStrings[i]);
		}
	}

	protected String[] showCourse(String courseNum) {
		// TODO Auto-generated method stub
		String[] courseInfoStrings = null;

		String courseInfoString = IOHelper.findOneRecord(0, "courseList.txt",
				courseNum);
		if (courseInfoString == null) {
			return null;
		} else {
			courseInfoStrings = courseInfoString.split(" ");
		}
		return courseInfoStrings;
	}

	public boolean hasRight(String courseNum) {
		String string = null;
		boolean hasRight = false;
		string = IOHelper.findOneRecord(0, "teachersList.txt", this.userID);
		String[] strings = string.split(" ");
		for (int i = 0; i < strings.length; i++) {
			System.out.println(strings[i]);
			if (strings[i].equals(courseNum)) {
				hasRight = true;
				break;
			}
		}
		return hasRight;
	}

	public void displayPanel(ActionEvent e) {
		displayPanel.removeAll();
		System.out.println(operationButtons.indexOf(e.getSource()));
		displayPanel.add(operatingPanels.get(operationButtons.indexOf(e
				.getSource())));
		this.repaint();
	}

}

class FormatHintButton extends JButton {
	protected Font middlFont = new Font("serif", Font.BOLD, 20);

	public FormatHintButton() {

	}

	public FormatHintButton(int x, int y, int length, int width) {
		// TODO Auto-generated constructor stub
		super("Format Hint");
		this.setFont(middlFont);
		this.setBounds(x, y, length, width);
		this.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String formatInfo = "";
				formatInfo += "(1)You need to fill the T's name and T's Num field both like this 001&002 Jack&Lily\n"
						+ "(2)Pay attention that the T's name should be strictly compitable to the T' Num\n"
						+ "(3)The assistent's num field should only allowed to be mf1000&mf1002 like this if there is more than one assastent\n";
				JOptionPane.showMessageDialog(null, formatInfo);
			}
		});
	}
}
